﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace FirstGame
{
    public class VolumeControl : MenuItem
    {
        private int _Level;

        public int Level
        {
            get { return _Level; }
            set { _Level = value; }
        }

        private ButtonItem _IncreaseButton;

        public ButtonItem IncreaseButton
        {
            get { return _IncreaseButton; }
            set { _IncreaseButton = value; }
        }
        private ButtonItem _DecreaseButton;

        public ButtonItem DecreaseButton
        {
            get { return _DecreaseButton; }
            set { _DecreaseButton = value; }
        }

        private ButtonItem _IconBar;

        public ButtonItem IconBar
        {
            get { return _IconBar; }
            set { _IconBar = value; }
        }
        public event EventHandler LevelChanged;
        public void LevelChange()
        {
            if (LevelChanged != null)
                LevelChanged(this, EventArgs.Empty);
        }

        public VolumeControl(ContentManager content,MenuScreen parent, string[] strTextureIncre,
            string[] strTextureDecre, string inconBar, Vector2 topleft)            
        {
            TopLeft = topleft;
            _InitTopleft = topleft;
            _CurrentTopleft = _InitTopleft;
            _Level = 0;
            _Menuscreen = parent;
            Vector2 temp = new Vector2(topleft.X + 5*(30+5), topleft.Y);
            _IncreaseButton = new ButtonItem(content,parent, strTextureIncre[0], strTextureIncre[1],temp ,temp, new Vector2(30, 30));
            _IncreaseButton.Selected += new EventHandler(_IncreaseButton_Selected);
            _DecreaseButton = new ButtonItem(content,parent, strTextureDecre[0], strTextureDecre[1], topleft,topleft, new Vector2(30, 30));
            _DecreaseButton.Selected += new EventHandler(_DecreaseButton_Selected);
            _IconBar = new ButtonItem(content,parent, inconBar, inconBar, Vector2.Zero, Vector2.Zero, new Vector2(30, 30));

        }

        void _DecreaseButton_Selected(object sender, EventArgs e)
        {
            _Level--;
            if (_Level < 0)
                _Level = 0;
            this.LevelChange();
        }

        void _IncreaseButton_Selected(object sender, EventArgs e)
        {
            _Level++;
            if (_Level > 4)
                _Level = 4;
            this.LevelChange();
        }

        public override void Update(GameTime gameTime, InputSystem input)
        {
            _IncreaseButton.Update(gameTime, input);
            _DecreaseButton.Update(gameTime, input);            
            
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {

            _IncreaseButton.Draw(gameTime, spriteBatch);
            _DecreaseButton.Draw(gameTime, spriteBatch);
            if(_Level != 0 )
            {
                for (int i = 0; i < _Level; i++)
                {
                    Vector2 temp = Vector2.Zero;
                    temp.X = _TopLeft.X + _DecreaseButton.Size.X  +i*(_IconBar.Size.X+5) + 5;
                    temp.Y = _TopLeft.Y;
                    _IconBar.TopLeft = temp;
                    _IconBar.Sprites[0].Position = temp;
                    _IconBar.Sprites[1].Position = temp;
                    _IconBar.Draw(gameTime, spriteBatch);
                }
            }
        }
    }
}
